package com.ilop.sthome.mvp.present.scene;

import android.content.Context;

import com.example.common.base.OnCallBackToRefresh;
import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.mvp.contract.scene.SceneEditContract;
import com.ilop.sthome.common.command.SendSceneCommand;
import com.ilop.sthome.ui.dialog.BaseDialog;
import com.ilop.sthome.ui.dialog.TipDialog;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.SceneDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.SceneSwitchDaoUtil;
import com.siterwell.familywellplus.R;

/**
 * @author skygge
 * @date 2020-01-03.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class SceneEditPresenter extends BasePresenterImpl<SceneEditContract.IView> implements SceneEditContract.IPresent {

    private Context mContext;
    private int mSceneId;
    private String mDeviceName;
    private String mName;
    private String mColors;
    private SceneBean mScene;
    private SendSceneCommand mSendScene;

    public SceneEditPresenter(Context mContext, String mDeviceName, int sceneId) {
        this.mContext = mContext;
        this.mSceneId = sceneId;
        this.mDeviceName = mDeviceName;
        DeviceInfoBean mDeviceInfo = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
        mSendScene = new SendSceneCommand(mContext, mDeviceInfo);
    }

    @Override
    public void refreshName() {
        mScene = SceneDaoUtil.getInstance().findSceneBySid(mSceneId, mDeviceName);
        mColors = mScene.getColor();
        switch (mSceneId){
            case 0:
                mName = mContext.getString(R.string.home_mode);
                break;
            case 1:
                mName = mContext.getString(R.string.out_mode);
                break;
            case 2:
                mName = mContext.getString(R.string.sleep_mode);
                break;
            default:
                mName = mScene.getModleName();
                break;

        }
        mView.showSceneName(mName);
        mView.showSceneColor(Integer.parseInt(mColors.substring(1)));
    }

    @Override
    public void setSceneColor(int color) {
        mColors = "F" + color;
    }

    @Override
    public void onSaveScene(String name) {
        this.mName = name;
        mScene.setModleName(name);
        mScene.setColor(mColors);
        String code = CoderUtils.increaseScene(mScene);
        mSendScene.increaseScene(code.toUpperCase());
    }

    @Override
    public void onSaveSuccess() {
        SceneDaoUtil.getInstance().getSceneDao().update(mScene);
        mView.showToastMsg(mContext.getString(R.string.modify_successfully));
        mView.finishActivity();
    }

    @Override
    public void deleteScene() {
        if (mSceneId < 3){
            String title = mContext.getString(R.string.system_mode_not_allow_delete);
            showDialog(title);
        }else {
            int mCurrentScene = SceneDaoUtil.getInstance().findSceneByChoice(mDeviceName).getSid();
            if (mSceneId == mCurrentScene){
                String title = mContext.getString(R.string.can_t_delete_the_current_scene);
                showDialog(title);
            }else {
                String sd = String.format(mContext.getResources().getString(R.string.want_to_delete_confirm_eq), mName);
                TipDialog dialog = new TipDialog(mContext, ()->{
                    mView.showProgress();
                    mSendScene.deleteScene(mSceneId);
                });
                dialog.setMsg(sd);
                dialog.show();
            }
        }
    }

    @Override
    public void deleteSceneSuccess(int sceneId) {
        SceneDaoUtil.getInstance().deleteBySid(sceneId, mDeviceName);
        SceneSwitchDaoUtil.getInstance().deleteAllSwitch(sceneId, mDeviceName);
        mView.showToastMsg(mContext.getString(R.string.delete_success));
        mView.finishActivity();
    }

    @Override
    public void whetherToSave(String scene) {
        BaseDialog mDialog = new BaseDialog(mContext, new OnCallBackToRefresh() {
            @Override
            public void onConfirm() {
                mView.showProgress();
                onSaveScene(scene);
            }

            @Override
            public void onCancel() {
                mView.finishActivity();
            }
        });
        mDialog.setMsg(mContext.getString(R.string.save_modle_or_not));
        mDialog.show();
    }

    private void showDialog(String title){
        BaseDialog mDialog = new BaseDialog(mContext);
        mDialog.setCancelVisibility(true);
        mDialog.setMsg(title);
        mDialog.setCancelText(mContext.getString(R.string.initialzation_guidance_button));
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.show();
    }
}
